package com.creative.accountsdk.net;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
/**
 * Created by yinjialiang on 15-4-1.
 */
public class XHttpHandler  extends JsonHttpResponseHandler {
    private Context context;
    private Dialog dialog;

    public XHttpHandler(Context context)
    {
        this(context, null);
    }
    public XHttpHandler(Context context, Dialog dialog)
    {
        this.context = context;
        this.dialog = dialog;
    }

    @Override
    public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
        super.onSuccess(statusCode, headers, response);
        Log.e("TAG", "成功后返回数据。。。。" + response.toString());
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
        super.onFailure(statusCode, headers, throwable, errorResponse);
        try {
            NetworkFailure.handle(context, statusCode, errorResponse);
            if(dialog != null) {
                dialog.cancel();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
        super.onFailure(statusCode, headers, throwable, errorResponse);
        try {
            NetworkFailure.handle(context, statusCode, errorResponse);
            if(dialog != null) {
                dialog.cancel();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
